<template>
  <div>
    <p>评估任务号为:{{this.ruleForm.evaluatetaskid}}</p>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="180px" class="demo-ruleForm">
<!--      <el-form-item label="评估任务号" prop="evaluatetaskid">-->
<!--        <el-input v-model="ruleForm.evaluatetaskid" readonly></el-input>-->
<!--      </el-form-item>-->
    </el-form>
  <div style="text-align: center">
    <el-transfer
        style="text-align: left; display: inline-block;"
        v-model="value"
        filterable
        :right-default-checked="[value]"
        :render-content="renderFunc"
        :titles="['未选择的学校', '已选择的学校']"

        :format="{
        noChecked: '${total}',
        hasChecked: '${checked}/${total}'
      }"
        @change="handleChange"
        :data="data">

    </el-transfer>
  </div>
  </div>
</template>

<script>
export default {
  data() {
    const generateData = _ => {
      const data = [];
      let len=0;
      axios.get("http://localhost:8081/school/findAll").then((res)=>{
        len=res.data.length;
        this.datalen=len
        for (let i = 0; i < len; i++) {
          data.push({
            key: i,
            label: res.data[i].schoolname,
          });
        }
        this.datacopy=data;
      })
      return data;
    };
    return {
      data: generateData(),
      datacopy:[],
      datalen:0,
      value: [],
      value4: [],
      existSchoolName:[],
      ruleForm: {
        evaluatetaskid:this.$route.query.evaluatetaskid,
      },
      renderFunc(h, option) {
        return <span>{ option.key } - { option.label }</span>;
      }
    };
  },

  methods: {
    //value:右边的值  direction：向左还是向右 movedKeys:移动的值的数组
    handleChange(value, direction, movedKeys) {
      if (direction==="right"){
        //如果向右移动 则加入数据库
        // console.log(this.data[movedKeys[0]].label)
        for (let ii=0;ii<movedKeys.length;ii++){
          let newInsert={evaluatetaskid: this.ruleForm.evaluatetaskid,schoolname:this.data[movedKeys[ii]].label}
          console.log(newInsert)
          axios.post("http://localhost:8081/evaluatetaskarrange/save",newInsert).then((res)=>{
            // console.log(newInsert)
            if (res.data.msg==="发布成功"){
              this.$message({
                message: '添加成功！',
                type: 'success'
                //添加成功后页面跳转
              });
              // this.$router.push({
              //   path:'/EvaluateTaskListForExpert',
              //   query:{
              //   count:this.value.length
              // }})
              axios.get("http://localhost:8081/evaluatetask/selectone/"+this.$route.query.id).then((res)=>{
                    let info=res.data
                info.selectedschoolnumber=this.value.length
                console.log(info)
                axios.put("http://localhost:8081/evaluatetask/update",info).then((res)=>{
                  if (res.data.msg==="更新成功"){
                  }
                  else{
                    this.$message({
                      message: '评估任务号重复或者评估号不存在！请重新输入',
                      type: 'fail'
                    });
                  }
                })

              })
              // this.$bus.$emit('count',this.value.length)
            }
            else{
              this.$message({
                message: '评估号重复！请重新输入',
                type: 'fail'
              });
            }
          })

        }
      }
      if (direction==="left"){
        //如果向左移动 则从数据库中删除
        console.log(movedKeys)
        console.log(this.data[movedKeys[0]].label)
        for (let jj=0;jj<movedKeys.length;jj++){
          axios.delete('http://localhost:8081/evaluatetaskarrange/delete/2/'+this.data[movedKeys[jj]].label).then(()=>{
            this.$message({
              message: '取消成功！',
              type: 'success'
              //添加成功后页面跳转
            });
            axios.get("http://localhost:8081/evaluatetask/selectone/"+this.$route.query.id).then((res)=>{
              let info=res.data
              info.selectedschoolnumber=this.value.length
              console.log(info)
              axios.put("http://localhost:8081/evaluatetask/update",info).then((res)=>{
                if (res.data.msg==="更新成功"){
                }
                else{
                  this.$message({
                    message: '评估任务号重复或者评估号不存在！请重新输入',
                    type: 'fail'
                  });
                }
              })

            })

            // location.reload();
          })
        }
      }
    }
  },
  created() {
    const a=localStorage.getItem("access-expert")
    if(!a){
      this.$alert('未登录','提示',{
        confirmButtonText:'确定'
      }).then((res)=>{
        this.$router.replace({path:'/login'})
      })
    }
    else {
      let len=0
      // axios.get("http://localhost:8081/evaluatetask/selectone/"+this.$route.query.id).then((res)=>{
      //   this.ruleForm.evaluatetaskid=res.data.evaluatetaskid
      // })

      axios.get("http://localhost:8081/evaluatetaskarrange/search/1/"+this.$route.query.evaluatetaskid).then((res)=>{
        len=res.data.length;
        while(len){
          len--;
          this.existSchoolName.push(
              res.data[len].schoolname
          )
        }
        for (let j=0;j<this.existSchoolName.length;j++){
          for (let k=0;k<this.datalen;k++){
            if (this.existSchoolName[j]===this.datacopy[k].label){
              this.value.push(this.datacopy[k].key)
            }
          }
        }
        // console.log(this.value)
      })
    }
  }
};
</script>
<style scoped>

</style>
